package com.wbao.dianniu.ui;

import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.SurfaceView;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import com.google.zxing.Result;
import com.google.zxing.client.android.AutoScannerView;
import com.google.zxing.client.android.BaseCaptureActivity;
import com.wbao.dianniu.R;
import com.wbao.dianniu.data.QrCodeData;
import com.wbao.dianniu.logical.Const;
import com.wbao.dianniu.logical.GlobalContext;
import com.wbao.dianniu.utils.Utils;

/**
 * Created by Administrator on 2017/8/9.
 */

public class WeChatCaptureActivity extends BaseCaptureActivity {
    private static final String TAG = WeChatCaptureActivity.class.getSimpleName();

    private SurfaceView surfaceView;
    private AutoScannerView autoScannerView;
    private ImageView backIV;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_wechat_capture);
        surfaceView = (SurfaceView) findViewById(R.id.preview_view);
        backIV = (ImageView) findViewById(R.id.back_wechat_iv);
        autoScannerView = (AutoScannerView) findViewById(R.id.autoscanner_view);
        backIV.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                WeChatCaptureActivity.this.finish();
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        autoScannerView.setCameraManager(cameraManager);
    }

    @Override
    public SurfaceView getSurfaceView() {
        return (surfaceView == null) ? (SurfaceView) findViewById(R.id.preview_view) : surfaceView;
    }

    @Override
    public void dealDecode(Result rawResult, Bitmap barcode, float scaleFactor) {
        playBeepSoundAndVibrate(true, false);
        // 对扫描结果进行解析，如果是内容内容 则跳转到用户详情页
        if (!TextUtils.isEmpty(rawResult.getText())) {
            Gson gson = new Gson();
            QrCodeData data = null;
            try {
                data = gson.fromJson(rawResult.getText(), QrCodeData.class);
            } catch (JsonSyntaxException e) {
                e.printStackTrace();
            }
            if (null != data) {
                String id = data.getAccountId();
                if (null != id) {
                    int _id = Integer.valueOf(id);
                    Utils.intoHeadInfoActivity(_id, this);
                    this.finish();
                } else {
                    intentScanResult(rawResult.getText());
                }
            } else {
                intentScanResult(rawResult.getText());
            }
        } else {
            Toast.makeText(this, getResources().getString(R.string.result_null), Toast.LENGTH_LONG).show();
        }
    }

    private void intentScanResult(String str) {
        // 跳转到扫描结果页面
        Intent intent = new Intent(WeChatCaptureActivity.this, ScanResultActivity.class);
        intent.putExtra(Const.NICE_NAME_INTENT, str);
        startActivity(intent);
        this.finish();
    }

}
